package com.secsbrain.development.web.controller;

import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.secsbrain.cmdb.client.iface.HostInfoService;
import com.secsbrain.common.ResponseResult;
import com.secsbrain.common.utils.DateTimeUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.secsbrain.common.ResponseVo;
import com.secsbrain.cmdb.client.model.App;
import com.secsbrain.cmdb.client.model.HostDetail;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("api/statistics")
@ApiIgnore
public class StatisticsController {
    @Autowired
    private HostInfoService hostInfoService;
    
    @GetMapping("server")
    public ResponseResult serverStatistics() {
    	ResponseVo<List<HostDetail>> serverDetailVo = hostInfoService.queryAllHost();
    	if(!serverDetailVo.isSuccess()){
    	    return new ResponseResult<>(serverDetailVo.getErrorCode(),serverDetailVo.getMsg());
    	}
    	
    	List<HostDetail> serverDetailDTOS=serverDetailVo.getData();
    	
		serverDetailDTOS.sort(new Comparator<HostDetail>() {

			@Override
			public int compare(HostDetail o1, HostDetail o2) {
				Date time1=o1.getUpdateTime();
				Date time2=o2.getUpdateTime();
				if(time1!=null&&time2!=null){
					return time2.compareTo(time1);
				}
				return 1;
			}
		});
    	
    	
        Map<String, Object> result = Maps.newHashMap();
        
		result.put("total", serverDetailDTOS.size());
        result.put("updatedTime", DateTimeUtils.dateParseString(CollectionUtils.isEmpty(serverDetailDTOS)?new Date():serverDetailDTOS.get(0).getUpdateTime()));

        Map<String, Integer> stateMap = Maps.newHashMap();
        result.put("state", stateMap);

        Map<String, Integer> envMap = Maps.newHashMap();
        result.put("env", envMap);

        serverDetailDTOS.forEach(s -> {

	      	List<App> appList=s.getAppList();
            String stateKey = appList.size()>0?"INUSE":"IDLE";
            Integer stateCount = stateMap.getOrDefault(stateKey, 0);
            stateMap.put(stateKey, ++stateCount);

            String envKey = StringUtils.isBlank(s.getEnvName()) ? "none" : s.getEnvName();
            Integer envCount = envMap.getOrDefault(envKey, 0);
            envMap.put(envKey, ++envCount);
        });
        return new ResponseResult<>(result);
    }

    @GetMapping("deploy")
    public ResponseResult publishQueueStatistics() {

        Map<String, Object> result = Maps.newHashMap();

        result.put("updatedTime", DateTimeUtils.dateParseString(new Date()));
        result.put("deployPool", new HashMap<String, Object>());

        List<Map<String, Object>> checking = Lists.newArrayList();
        result.put("checkQueue", checking);
        List<Map<String, Object>> hangup = Lists.newArrayList();
        result.put("hangupQueue", hangup);

        Map<String, Object> test = new HashMap<String, Object>();
        result.put("testPool", test);

        Map<String, Object> hangupPool = new HashMap<String, Object>();
        result.put("hangupPool", hangupPool);

        Map<String, Object> scheduledCheckerPool = new HashMap<String, Object>();
        result.put("scheduledCheckerPool", scheduledCheckerPool);
        return new ResponseResult<>(result);
    }
    
}
